<?php
$initTitle = "South Salem High School - Courses";
$style="<link href=\"http://www.southsaxons.com/sshs.css\" rel=\"stylesheet\" type=\"text/css\" /><style>\n .opacityFont array(\n filter:alpha(opacity=75);\n -moz-opacity:.75; \n opacity:.75; \n ) \n .opacity array(\n background-color: #FFF;\n filter:alpha(opacity=75);\n -moz-opacity:.75;\n opacity:.75;\n border: 1px solid #000;\n color: #000;\n )\n </style>";
$id = "1";
echo "<HTML><HEAD><TITLE>$initTitle</TITLE>$style";

$db_host = 'localhost';
$db_user = 'pepgsou_admin';
$db_pwd = '_20pepg09';
$database = 'pepgsou_main';
$table = 'JL';
if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");




if (isset($insertid))
{
	$iid = $insertid;
	if (mysql_num_rows(mysql_query("SELECT * FROM stu_course_link WHERE stu_id='$id' AND course_id='$iid'"))==0)
	{
		mysql_query("INSERT INTO stu_course_link (stu_id, course_id) VALUES ('$id', '$iid')");
	}

}

if (isset($removeid))
{
	$rid = $removeid - 1000;
	mysql_query("DELETE FROM stu_course_link WHERE stu_id='$id' AND course_id='$rid'");
}

if (isset($_POST["search"]))
{
	$search = $_POST["search"];
}
?>
<script language="javascript">
var list = [];
var output = "";
var output2 = "";
var dropdown = "";
var oldactive = 0;
function makeBubble(id, contents, dep, insert, credit, repeatable, mingrade, maxgrade, prereq, description)
{
	var o  = "<tr><td>";
        o += "<div id=\""+id+"\" style=\"width:400px;height:150px;background-color:#FFFFFF;border-style:solid;border-width:1;border-color:#000000;\" onclick=\"pressed(" + id + "," + insert + ");\">";
	    o += "&nbsp;" + contents;
		o += "<br><font size=1>" + description+ "</font>";
		o += "<font size=2><br>Grade: "+mingrade+"-"+maxgrade+"</font>";
	    o += "</div>";
	    o += "</td></tr>";
	list[id] = [];
	list[id][0] = contents;
	list[id][1] = dep;
	return o;
}
function pressed(id, insert)
{
	var l = document.getElementById(id);
	var search = "";
	var dp = "";
	<?
		if (isset($search))
		{
			echo "search = 'search=$search&';\n";
		}
		if (isset($dept))
		{
			echo "dp = 'dept=$dept&';\n";
		}
	?>
	if (insert)
		document.location="?"+dp+search+"insertid="+id;
	else
		document.location="?"+dp+search+"removeid="+id;
}
</script>
<?
echo "</HEAD>";
?>
<body>
<?php

echo "<script language=\"javascript\">";
$esearch = "";
if (isset($dept))
{
	$esearch = "AND dept='$dept'";
}
if (isset($search))
{
	$esearch = "AND (title LIKE '$search%' OR title LIKE '%$search%' OR title like '%$search')";
}
$q = mysql_query("SELECT seq, Title, dept, credit, repeatable, mingrade, maxgrade, prereq, description FROM JL WHERE SEQ NOT IN (SELECT course_id FROM stu_course_link WHERE stu_id = '$id') $esearch ");
for ($i = 0; $i<mysql_num_rows($q); $i++)
{
	$name = mysql_result($q, $i, "title");
	$dept = mysql_result($q, $i, "dept");
	$seq = mysql_result($q, $i, "SEQ");
	$credit = mysql_result($q, $i, "credit");
	$repeatable = mysql_result($q, $i, "repeatable");
	$mingrade = mysql_result($q, $i, "mingrade");
	$maxgrade = mysql_result($q, $i, "maxgrade");
	$prereq = mysql_result($q, $i, "prereq");
	$description = mysql_result($q, $i, "description");
	echo "output += makeBubble($seq, \"$name\", \"$dept\", true, \"$credit\", \"$repeatable\", \"$mingrade\", \"$maxgrade\", \"$prereq\", \"$description\");";
}
echo "dropdown = \"<select onchange=\\\"document.location='?dept='+this.value;\\\">\";";
$q2 = mysql_query("SELECT seq, Title, dept, credit, repeatable, mingrade, maxgrade, prereq, description FROM JL WHERE SEQ IN (SELECT course_id FROM stu_course_link WHERE stu_id = '$id')");
$tdept = "";
for ($i = 0; $i<mysql_num_rows($q2); $i++)
{
	$name = mysql_result($q2, $i, "title");
	$dept = mysql_result($q2, $i, "dept");
	$seq = mysql_result($q2, $i, "SEQ");
	$credit = mysql_result($q2, $i, "credit");
	$repeatable = mysql_result($q2, $i, "repeatable");
	$mingrade = mysql_result($q2, $i, "mingrade");
	$maxgrade = mysql_result($q2, $i, "maxgrade");
	$prereq = mysql_result($q2, $i, "prereq");
	$description = mysql_result($q2, $i, "description");
	echo "output2 += makeBubble($seq+1000, \"$name\", \"$dept\", false, \"$credit\", \"$repeatable\", \"$mingrade\", \"$maxgrade\", \"$prereq\", \"$description\");";

}

$q3 = mysql_query("SELECT DISTINCT dept FROM JL");
for ($i = 0; $i<mysql_num_rows($q3); $i++)
{
	$dept = mysql_result($q3, $i, "dept");
	echo "dropdown += '<option value=\"$dept\">$dept</option>';";
}
echo "dropdown += '</select>';";
?>
document.write('<div style="position:absolute;top:60px;left:5%;">');
document.write("<table style=\"background-color:#FFFFFF;\" cellpadding=1 cellspacing=1>"+output2+"</table>");
document.write('</div>');
document.write('<div id="mainbox" style="position:absolute;top:60px;left:55%;">');
document.write(dropdown);
document.write('<form action="?" method="post"><input name="search" type="text" id="search"><input type="submit"></form>');
document.write("<table style=\"background-color:#FFFFFF;\" cellpadding=1 cellspacing=1>"+output+"</table>");
document.write('</div>');
<?
echo "</script>";
?>

</body></html>